use crate::errors::Error;
use log;
use syslog;

pub fn log_init(level: &str) -> Result<(), Error> {
    let log_level = match level {
        "debug" => log::LevelFilter::Info,
        _ => log::LevelFilter::Info,
    };

    let formatter = syslog::Formatter3164 {
        facility: syslog::Facility::LOG_USER,
        hostname: None,
        process: "rustime".into(),
        pid: 0,
    };
    let logger = syslog::unix(formatter).expect("could not connect to syslog");
    log::set_boxed_logger(Box::new(syslog::BasicLogger::new(logger)))
        .map(|()| log::set_max_level(log_level))?;
    Ok(())
}
